package com.youxue.teachin.jpush;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.app.Application;
import android.app.Application.ActivityLifecycleCallbacks;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.TextUtils;
import cn.jpush.android.api.JPushInterface;
import com.youxue.teachin.common.utils.PreContent;
import com.youxue.teachin.common.utils.PreferencesUtils;
import com.youxue.teachin.user.userSystem.UserSystem;
import rx.Observable;
import rx.functions.Func1;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-29 20:58
 * @version: 9.1.0
 */
public class JpushManager {
    /**
     * get App versionCode
     *
     * @param context
     * @return
     */
    public static String getVersionCode(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageInfo;
        String versionCode = "";
        try {
            packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionCode = packageInfo.versionCode + "";
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * get App versionName
     *
     * @param context
     * @return
     */
    public static String getVersionName(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageInfo;
        String versionName = "";
        try {
            packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    private static void setTag(final Application application) {
        Observable.timer(5000, TimeUnit.MILLISECONDS)
            .map(new Func1<Long, Void>() {
                @Override
                public Void call(Long aLong) {
                    try {
                        ApplicationInfo appInfo = null;
                        appInfo = application.getPackageManager()
                            .getApplicationInfo(application.getPackageName(),
                                PackageManager.GET_META_DATA);
                        String msg = appInfo.metaData.getString("UMENG_CHANNEL");
                        Set<String> tags = new HashSet<>();
                        tags.add(msg);
                        if (UserSystem.getInstance().isLogin()) {
                            String loginTag = PreferencesUtils.getString(PreContent.LOGIN_THREAD, "");
                            if (!TextUtils.isEmpty(loginTag)) {
                                tags.add(loginTag);
                            }
                        }
                        tags.add(getVersionCode(application));
                        tags.add(getVersionName(application));
                        JPushInterface.addTags(application, (int)System.currentTimeMillis(), tags);
                    } catch (NameNotFoundException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            });
    }

    public static void init(final Application application, boolean isDebug) {
        JPushInterface.setDebugMode(isDebug);
        JPushInterface.init(application);
        setTag(application);

        //省点模式3.0.9
        //JPushInterface.setPowerSaveMode();

        application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
                JPushInterface.onResume(activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {
                JPushInterface.onPause(activity);
            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }
}
